﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

namespace QuanLyThuNgan
{
    public partial class Frm_ChiTietXacNhanNopTien : DevExpress.XtraEditors.XtraForm
    {
        public Frm_ChiTietXacNhanNopTien()
        {
            InitializeComponent();
        }

        int iTongDaXN = 0;
        int iTongHoanTien = 0;
        int iTongChuaXN = 0;
        int iTongHoanChuaXN = 0;
        int iTongChiPhi = 0;
        CaThuNganDTO caThuNganDto = new CaThuNganDTO();
        CaThuNganCTL caThuNganCtl = new CaThuNganCTL();
        public string sNguoiThuTien, sNgayThu, sCaThu, sNoiThu, sNguoiNhan, sNgayNop;
        public int iIDCaThuNgan;
        private void Frm_ChiTietXacNhanNopTien_Load(object sender, EventArgs e)
        {
            txtTenThuNgan.Text = sNguoiThuTien;
            txtNgayThu.Text = sNgayThu;
            txtTenCaThu.Text = sCaThu;
            txtNoiThu.Text = sNoiThu;
            txtNguoiNhan.Text = sNguoiNhan;
            txtNgayNop.Text = sNgayNop;
            if (txtNguoiNhan.Text != "")
            {
                btnXacNhanThuTien.Enabled = false;
            }
            else
            {
                btnXacNhanThuTien.Enabled = true;
            }

            DataTable dtct = caThuNganCtl.m_SelectChiTietNopTien(iIDCaThuNgan);
            gdChiTietXacNhan.DataSource = dtct;
            //set readonly gridview----------
            m_ReadOnlyValue();
            
            //tinh tong gia tri tien-------------------------------------------------------
            for (int i = 0; i < dtct.Rows.Count; i++)
            {

                if (dtct.Rows[i]["loai"].ToString() == "1")
                {
                    iTongDaXN += Uit.it_Parse.ToInteger(dtct.Rows[i]["DaXacNhan"].ToString());
                    iTongChuaXN += Uit.it_Parse.ToInteger(dtct.Rows[i]["ChuaXacNhan"].ToString());
                }
                else
                {
                    iTongHoanTien += Uit.it_Parse.ToInteger(dtct.Rows[i]["DaXacNhan"].ToString());
                    iTongHoanChuaXN += Uit.it_Parse.ToInteger(dtct.Rows[i]["ChuaXacNhan"].ToString());
                }
              
            }

            //tong tien-----------------------
            iTongChiPhi = iTongDaXN - iTongHoanTien;                          
            
            //----------------------
            GridColumnSummaryItem item = new GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Custom, "ChuaXacNhan", "Tổng Tiền Phải Nộp: "+iTongChiPhi.ToString()+" ");
            gvChiTietXacNhan.Columns["ChuaXacNhan"].Summary.Add(item);

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //set readonly value--------------------
        void m_ReadOnlyValue()
        {
            txtTenThuNgan.Properties.ReadOnly = true;
            txtNgayThu.Properties.ReadOnly = true;
            txtTenCaThu.Properties.ReadOnly = true;
            txtNoiThu.Properties.ReadOnly = true;
            txtNguoiNhan.Properties.ReadOnly = true;
            txtNgayNop.Properties.ReadOnly = true;
        }

        private void btnDSNopTien_Click(object sender, EventArgs e)
        {
            frmMain frmain = new frmMain();
            Form f = FormHelpers.CheckChildFormShow("Frm_XacNhanNopTien", frmain);
            if (f == null)
            {
                Frm_XacNhanNopTien frm = new Frm_XacNhanNopTien();
                frm.MdiParent = this.ParentForm;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
            else
            {
                f.Activate();
                f.Focus();
            }

         //   this.Close();
        }

        private void btnXacNhanThuTien_Click(object sender, EventArgs e)
        {
            if (txtNguoiNhan.Text == "" || txtNgayNop.Text == "")
            {                
                caThuNganDto.IIDCaThuNgan = iIDCaThuNgan;
                caThuNganDto.IIDNguoiNhanTien = frmLogin.nd.IDNguoiDung;
                caThuNganDto.ITongSoTien = iTongChiPhi;
                caThuNganDto.ITienDaXacNhan = iTongDaXN;
                caThuNganDto.ITienChuaXacNhan = iTongChuaXN;
                caThuNganDto.IHoanTien = iTongHoanTien;
                caThuNganDto.DaNgayNopTien = Uit.it_Parse.ToDateTime(Uit.it_Date.CurrentDate()).ToString("yyyy-MM-dd");
                caThuNganDto.SsGioNopTien = Uit.it_Date.CurrentTime();
                //update xac nhan thu tien-----------------------------
                caThuNganCtl.m_UpdateXacNhanThuTien(caThuNganDto);
                XtraMessageBox.Show("Update thu tien thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                XtraMessageBox.Show("Dữ liệu đã xác nhận", "Thông báo", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }


    }
}